package com.xinbochuang.machine.admin.service.impl.macRoom;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinbochuang.machine.admin.domain.SnapshotEquCabinet;
import com.xinbochuang.machine.admin.domain.SnapshotEquipmentDetail;
import com.xinbochuang.machine.admin.domain.macRoom.*;
import com.xinbochuang.machine.admin.domain.macRoom.json.Node;
import com.xinbochuang.machine.admin.domain.plan.CheckPlan;
import com.xinbochuang.machine.admin.domain.plan.CheckPlanRoom;
import com.xinbochuang.machine.admin.domain.plan.CheckRoomCabinet;
import com.xinbochuang.machine.admin.domain.vo.CabinetSubBean;
import com.xinbochuang.machine.admin.dto.EquCabinetDto;
import com.xinbochuang.machine.admin.dto.EquipmentDetailDto;
import com.xinbochuang.machine.admin.mapper.SnapshotEquCabinetMapper;
import com.xinbochuang.machine.admin.mapper.SnapshotEquipmentDetailMapper;
import com.xinbochuang.machine.admin.mapper.macRoom.*;
import com.xinbochuang.machine.admin.mapper.plan.CheckPlanMapper;
import com.xinbochuang.machine.admin.mapper.plan.CheckPlanRoomMapper;
import com.xinbochuang.machine.admin.mapper.plan.CheckRoomCabinetMapper;
import com.xinbochuang.machine.admin.service.macRoom.IEquCabinetService;
import com.xinbochuang.machine.admin.service.macRoom.IMacShowService;
import com.xinbochuang.machine.common.utils.SecurityUtils;
import com.xinbochuang.machine.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.awt.geom.Point2D;
import java.util.*;

/**
 * @author 杜文博
 * @date 2020-09-08
 */
@Service
@Slf4j
public class EquCabinetServiceImpl extends ServiceImpl<EquCabinetMapper, EquCabinet> implements IEquCabinetService {

    @Autowired
    EquCabinetMapper equCabinetMapper;
    @Autowired
    EnergyMapper energyMapper;
    @Autowired
    EquipmentMapper equipmentMapper;

    @Autowired
    CheckRoomCabinetMapper checkRoomCabinetMapper;

    @Autowired
    CheckPlanRoomMapper checkPlanRoomMapper;

    @Autowired
    CheckPlanMapper checkPlanMapper;

    @Autowired
    ContrastEquCabinetMapper contrastEquCabinetMapper;

    @Autowired
    ContrastEquipmentDetailMapper contrastEquipmentDetailMapper;

    @Autowired
    SnapshotEquCabinetMapper snapshotEquCabinetMapper;

    @Autowired
    SnapshotEquipmentDetailMapper snapshotEquipmentDetailMapper;
    @Autowired
    EquipmentTypeMapper equipmentTypeMapper;

    @Autowired
    EquipmentDetailMapper equipmentDetailMapper;

    @Resource
    private IMacShowService pointsService;

    @Override
    public List<EquCabinetDto> findEquCabinetDetail(String macRoom) {
        List<EquCabinetDto> list = equCabinetMapper.findEquCabinetDetail(macRoom);
        return list;

    }

    @Override
    public List<EquCabinetDto> findEquCabinetByName(String binetName) {
        List<EquCabinetDto> list = equCabinetMapper.findEquCabinetByName(binetName);
        return list;
    }

    @Override
    public IPage<EquCabinet> findEquCabinetListByName(EquCabinet equCabinet) {
        QueryWrapper<EquCabinet> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(EquCabinet::getDeleteFlag, 0);
        queryWrapper.lambda().orderByAsc(EquCabinet::getUuid);
        if (equCabinet != null) {
            if (StrUtil.isNotBlank(equCabinet.getCabinetName())) {
                queryWrapper.lambda().like(EquCabinet::getCabinetName, equCabinet.getCabinetName());
            }
            if (StrUtil.isNotBlank(equCabinet.getMacId())) {
                queryWrapper.lambda().like(EquCabinet::getMacId, equCabinet.getMacId());
            }
        }
        return page(new Page<>(equCabinet.getPageNum(), equCabinet.getPageSize()), queryWrapper);
    }

    @Override
    public Map<String, Object> getDetialById(EquCabinet equCabinet) {
        Map<String, Object> result = new HashMap<>();
        equCabinet = equCabinetMapper.selectById(equCabinet.getUuid());
        if (equCabinet != null) {
            List<Energy> energyList = energyMapper.selectList(new QueryWrapper<Energy>().lambda().eq(Energy::getCabinetId, equCabinet.getUuid()));
            if (!energyList.isEmpty()) {
                result.put("energy", energyList.get(0));
            }
            List<EquipmentDetailDto> equipment = equipmentDetailMapper.selectByCabinetId(equCabinet.getUuid());
            result.put("equipment", equipment);
        }
        result.put("cabinet", equCabinet);
        return result;
    }

    //	@Override
//	public void appEdit(CabinetSubBean cabinetSubBean) {
//
//	}
    @Transactional
    @Override
    public void contractInsert(CabinetSubBean cabinetSubBean) {
        String cabinetId = cabinetSubBean.getCabinet().getUuid();
        String contrastCabinetId = UUID.randomUUID().toString().trim().replaceAll("-", "");
        ContrastEquCabinet contrastEquCabinet = new ContrastEquCabinet();
        EquipmentDetailDto equipmentDetail = new EquipmentDetailDto();
        SnapshotEquCabinet snapshotEquCabinet = new SnapshotEquCabinet();
        SnapshotEquipmentDetail snapshotEquipmentDetail = new SnapshotEquipmentDetail();
        //若前端机柜Id为空则视为新增，直接将数据插入核查表快照表不作处理
        if (StringUtils.isBlank(cabinetId)) {
            cabinetId = contrastCabinetId;
            contrastEquCabinet.setUuid(contrastCabinetId);
            contrastEquCabinet.setMacId(cabinetSubBean.getCabinet().getMacId());
            contrastEquCabinet.setPlanId(cabinetSubBean.getPlanId());
            contrastEquCabinet.setCreateTime(DateUtil.now());
            contrastEquCabinet.setUpdateTime(DateUtil.now());
            contrastEquCabinet.setCabinetId(cabinetId);
            contrastEquCabinet.setEnergyName(cabinetSubBean.getEnergy().getEnergyName());
            contrastEquCabinet.setCabinetName(cabinetSubBean.getCabinet().getCabinetName());
            contrastEquCabinet.setUlocationQuantity(cabinetSubBean.getCabinet().getUlocationQuantity());
            contrastEquCabinet.setOptType(cabinetSubBean.getCabinet().getOptType());
            //核验机柜数据并插入
            contrastEquCabinetMapper.insert(contrastEquCabinet);
        } else {
            //通过机柜Id查询机柜数据
            EquCabinet equCabinet = equCabinetMapper.selectById(cabinetId);
            //机柜未变更状态为0，机柜名称orU位变更状态为1
            if (equCabinet.getCabinetName().equals(cabinetSubBean.getCabinet().getCabinetName()) && equCabinet.getUlocationQuantity().equals(cabinetSubBean.getCabinet().getUlocationQuantity())) {
                contrastEquCabinet.setOwnState(0);
            } else if (!equCabinet.getCabinetName().equals(cabinetSubBean.getCabinet().getCabinetName()) || !equCabinet.getUlocationQuantity().equals(cabinetSubBean.getCabinet().getUlocationQuantity())) {
                contrastEquCabinet.setOwnState(1);
            }
            contrastEquCabinet.setUuid(contrastCabinetId);
            contrastEquCabinet.setMacId(cabinetSubBean.getCabinet().getMacId());
            contrastEquCabinet.setPlanId(cabinetSubBean.getPlanId());
            contrastEquCabinet.setCreateTime(DateUtil.now());
            contrastEquCabinet.setUpdateTime(DateUtil.now());
            contrastEquCabinet.setEnergyName(cabinetSubBean.getEnergy().getEnergyName());
            contrastEquCabinet.setCabinetName(cabinetSubBean.getCabinet().getCabinetName());
            contrastEquCabinet.setCabinetId(cabinetSubBean.getCabinet().getUuid());
            contrastEquCabinet.setUlocationQuantity(cabinetSubBean.getCabinet().getUlocationQuantity());
            contrastEquCabinet.setOptType(cabinetSubBean.getCabinet().getOptType());
            //核验机柜数据并插入
            contrastEquCabinetMapper.insert(contrastEquCabinet);
            //将原始机柜数据存入快照表
            snapshotEquCabinet.setOldCabinetName(equCabinet.getCabinetName());
            snapshotEquCabinet.setOldEnergyName(equCabinet.getEnergyName());
            snapshotEquCabinet.setOldMacId(equCabinet.getMacId());
            snapshotEquCabinet.setCabientId(equCabinet.getUuid());
            snapshotEquCabinet.setOldUlocationQuantity(equCabinet.getUlocationQuantity());
            snapshotEquCabinet.setPlanId(cabinetSubBean.getPlanId());
            snapshotEquCabinet.setUuid(UUID.randomUUID().toString().trim().replaceAll("-", ""));
            snapshotEquCabinet.setCreateTime(DateUtil.now());
            snapshotEquCabinet.setUpdateTime(DateUtil.now());
            snapshotEquCabinetMapper.insert(snapshotEquCabinet);
        }

        //资产数据处理
        List<EquipmentDetailDto> list = cabinetSubBean.getEquipment();
        if (list.size() > 0) {
            boolean isChange = false;
            for (int i = 0; i < list.size(); i++) {
                EquipmentDetailDto equipmentDetailDto = list.get(i);
                if (equipmentDetailDto.getOptType().equals("1") || equipmentDetailDto.getOptType().equals("2")) {
                    isChange = true;
                }
                ContrastEquipmentDetail contrastEquipmentDetail = new ContrastEquipmentDetail();
                //若设备Id为空贼视为新增数据直接插入核对表快照表不做处理
                if (StringUtils.isBlank(equipmentDetailDto.getUuid())) {
                    String contrastEquId = UUID.randomUUID().toString().trim().replaceAll("-", "");
                    contrastEquipmentDetail.setAssetsBigType(equipmentDetailDto.getAssetsBigType());
                    contrastEquipmentDetail.setAssetsSmallType(equipmentDetailDto.getAssetsSmallType());
                    contrastEquipmentDetail.setAssetsNumber(equipmentDetailDto.getAssetsNumber());
                    contrastEquipmentDetail.setOwnerSystem(equipmentDetailDto.getOwnerSystem());
                    contrastEquipmentDetail.setModelType(equipmentDetailDto.getModelType());
                    contrastEquipmentDetail.setAssetsState(equipmentDetailDto.getAssetsState());
                    contrastEquipmentDetail.setPrincipal(equipmentDetailDto.getPrincipal());
                    contrastEquipmentDetail.setDepartment(equipmentDetailDto.getDepartment());
                    contrastEquipmentDetail.setOffice(equipmentDetailDto.getOffice());
                    contrastEquipmentDetail.setMacRoomName(equipmentDetailDto.getMacRoomName());
                    //原始数据为空时机柜Id用对应生成的机柜Id
                    contrastEquipmentDetail.setCabinetId(cabinetId);
                    contrastEquipmentDetail.setOpenDate(equipmentDetailDto.getOpenDate());
                    contrastEquipmentDetail.setUfloorNum(equipmentDetailDto.getUfloorNum());
                    contrastEquipmentDetail.setCabinetName(equipmentDetailDto.getCabinetName());
                    //设备Id为空时EquId不做插入
                    if (StringUtils.isNotBlank(equipmentDetailDto.getEquId())) {
                        contrastEquipmentDetail.setEquId(equipmentDetailDto.getEquId());
                    } else {
                        contrastEquipmentDetail.setEquId(UUID.randomUUID().toString().trim().replaceAll("-", ""));
                    }

                    if (StringUtils.isBlank(contrastEquipmentDetail.getOffice())) {
                        contrastEquipmentDetail.setContrastState(0);
                    } else {
                        contrastEquipmentDetail.setContrastState(1);
                        isChange = true;
                    }
                    contrastEquipmentDetail.setPlanId(contrastEquCabinet.getPlanId());
                    contrastEquipmentDetail.setEquName(equipmentDetailDto.getEquName());
                    contrastEquipmentDetail.setEquState(equipmentDetailDto.getEquState());
                    contrastEquipmentDetail.setEquHeight(equipmentDetailDto.getEquHeight());
                    contrastEquipmentDetail.setUpdateTime(DateUtil.now());
                    contrastEquipmentDetail.setCreateTime(DateUtil.now());
                    contrastEquipmentDetail.setUuid(contrastEquId);
                    contrastEquipmentDetail.setOptType(equipmentDetailDto.getOptType());
                    //核验设备数据并插入
                    contrastEquipmentDetailMapper.insert(contrastEquipmentDetail);
                } else {
                    //根据Id查询相关资产信息
                    equipmentDetail = contrastEquipmentDetailMapper.findEquDetailByEquId(equipmentDetailDto.getEquId());
//				if(StringUtils.isNull(equipmentDetail)){
//					continue;
//				}
                    if (equipmentDetail.getAssetsBigType().equals(equipmentDetailDto.getAssetsBigType())
                            && equipmentDetail.getAssetsNumber().equals(equipmentDetailDto.getAssetsNumber())
                            && equipmentDetail.getAssetsSmallType().equals(equipmentDetailDto.getAssetsSmallType())
                            && equipmentDetail.getAssetsState().equals(equipmentDetailDto.getAssetsState())
                            && equipmentDetail.getCabinetName().equals(equipmentDetailDto.getCabinetName())
                            && equipmentDetail.getDepartment().equals(equipmentDetailDto.getDepartment())
                            && equipmentDetail.getModelType().equals(equipmentDetailDto.getModelType())
                            && equipmentDetail.getPrincipal().equals(equipmentDetailDto.getPrincipal())
                            && equipmentDetail.getOffice().equals(equipmentDetailDto.getOffice())
                            && equipmentDetail.getOpenDate().equals(equipmentDetailDto.getOpenDate())
                            && equipmentDetail.getOwnerSystem().equals(equipmentDetailDto.getOwnerSystem())
                            && equipmentDetail.getUfloorNum().equals(equipmentDetailDto.getUfloorNum())
                            && equipmentDetail.getEquName().equals(equipmentDetailDto.getEquName())
                            && equipmentDetail.getEquHeight().equals(equipmentDetailDto.getEquHeight())
                            && equipmentDetail.getEquState().equals(equipmentDetailDto.getEquState())
                            ) {
                        contrastEquipmentDetail.setContrastState(0);
                    } else {
                        contrastEquipmentDetail.setContrastState(1);
                        isChange = true;
                    }
                    String contrastEquId = UUID.randomUUID().toString().trim().replaceAll("-", "");
                    contrastEquipmentDetail.setAssetsBigType(equipmentDetailDto.getAssetsBigType());
                    contrastEquipmentDetail.setAssetsSmallType(equipmentDetailDto.getAssetsSmallType());
                    contrastEquipmentDetail.setAssetsNumber(equipmentDetailDto.getAssetsNumber());
                    contrastEquipmentDetail.setOwnerSystem(equipmentDetailDto.getOwnerSystem());
                    contrastEquipmentDetail.setModelType(equipmentDetailDto.getModelType());
                    contrastEquipmentDetail.setAssetsState(equipmentDetailDto.getAssetsState());
                    contrastEquipmentDetail.setPrincipal(equipmentDetailDto.getPrincipal());
                    contrastEquipmentDetail.setDepartment(equipmentDetailDto.getDepartment());
                    contrastEquipmentDetail.setOffice(equipmentDetailDto.getOffice());
                    contrastEquipmentDetail.setMacRoomName(equipmentDetailDto.getMacRoomName());
                    contrastEquipmentDetail.setCabinetId(equipmentDetailDto.getCabinetId());
                    contrastEquipmentDetail.setOpenDate(equipmentDetailDto.getOpenDate());
                    contrastEquipmentDetail.setUfloorNum(equipmentDetailDto.getUfloorNum());
                    contrastEquipmentDetail.setCabinetName(equipmentDetailDto.getCabinetName());
                    contrastEquipmentDetail.setEquId(equipmentDetailDto.getEquId());
                    contrastEquipmentDetail.setPlanId(contrastEquCabinet.getPlanId());
                    contrastEquipmentDetail.setEquName(equipmentDetailDto.getEquName());
                    contrastEquipmentDetail.setEquState(equipmentDetailDto.getEquState());
                    contrastEquipmentDetail.setEquHeight(equipmentDetailDto.getEquHeight());
                    contrastEquipmentDetail.setUpdateTime(DateUtil.now());
                    contrastEquipmentDetail.setCreateTime(DateUtil.now());
                    contrastEquipmentDetail.setUuid(contrastEquId);
                    contrastEquipmentDetail.setOptType(equipmentDetailDto.getOptType());
                    //核验设备数据并插入
                    contrastEquipmentDetailMapper.insert(contrastEquipmentDetail);
                    //将原始资产数据存入快照表
                    snapshotEquipmentDetail.setPlanId(cabinetSubBean.getPlanId());
                    snapshotEquipmentDetail.setOldAssetsBigType(equipmentDetail.getAssetsBigType());
                    snapshotEquipmentDetail.setOldAssetsNumber(equipmentDetail.getAssetsNumber());
                    snapshotEquipmentDetail.setOldAssetsSmallType(equipmentDetail.getAssetsSmallType());
                    snapshotEquipmentDetail.setOldAssetsState(equipmentDetail.getAssetsState());
                    snapshotEquipmentDetail.setOldCabinetId(equipmentDetail.getCabinetId());
                    snapshotEquipmentDetail.setOldCabinetName(equipmentDetail.getCabinetName());
                    snapshotEquipmentDetail.setOldDepartment(equipmentDetail.getDepartment());
                    snapshotEquipmentDetail.setOldEquId(equipmentDetail.getEquId());
                    snapshotEquipmentDetail.setOldOwnerSystem(equipmentDetail.getOwnerSystem());
                    snapshotEquipmentDetail.setOldModelType(equipmentDetail.getModelType());
                    snapshotEquipmentDetail.setOldPrincipal(equipmentDetail.getPrincipal());
                    snapshotEquipmentDetail.setOldOffice(equipmentDetail.getOffice());
                    snapshotEquipmentDetail.setOldMacRoomName(equipmentDetail.getMacRoomName());
                    snapshotEquipmentDetail.setOldOpenDate(equipmentDetail.getOpenDate());
                    snapshotEquipmentDetail.setOldUfloorNum(equipmentDetail.getUfloorNum());
                    snapshotEquipmentDetail.setOldEquName(equipmentDetail.getEquName());
                    snapshotEquipmentDetail.setOldEquHeight(equipmentDetail.getEquHeight());
                    snapshotEquipmentDetail.setOldEquState(equipmentDetail.getEquState());
                    snapshotEquipmentDetail.setCreateTime(DateUtil.now());
                    snapshotEquipmentDetail.setUpdateTime(DateUtil.now());
                    snapshotEquipmentDetail.setUuid(UUID.randomUUID().toString().trim().replaceAll("-", ""));
                    snapshotEquipmentDetailMapper.insert(snapshotEquipmentDetail);
                }
            }
            if (isChange) {
                contrastEquCabinet.setEquState(1);
                contrastEquCabinetMapper.updateById(contrastEquCabinet);
            }
        }
        //更改核验计划Id
        ContrastEquCabinet planContrastEquCabinet = cabinetSubBean.getCabinet();
        planContrastEquCabinet.setEnergyName(cabinetSubBean.getEnergy().getEnergyName());
        String cabinetUUID = UUID.randomUUID().toString().trim().replaceAll("-", "");
        //若机柜Id不为空则更改核验机柜关联表状态否则新增
        if (StringUtils.isNotBlank(planContrastEquCabinet.getUuid())) {
            planContrastEquCabinet.setUpdateTime(DateUtil.now());
            cabinetUUID = planContrastEquCabinet.getUuid();
            CheckRoomCabinet checkRoomCabinet = new CheckRoomCabinet();
            checkRoomCabinet.setPlanId(cabinetSubBean.getPlanId());
            checkRoomCabinet.setCabinetId(cabinetUUID);
            checkRoomCabinet.setRoomId(planContrastEquCabinet.getMacId());
            checkRoomCabinet.setStatus("1");
            checkRoomCabinetMapper.update(checkRoomCabinet, new QueryWrapper<CheckRoomCabinet>().lambda().eq(CheckRoomCabinet::getPlanId, cabinetSubBean.getPlanId()).eq(CheckRoomCabinet::getCabinetId, planContrastEquCabinet.getUuid()));
        } else {
            CheckRoomCabinet checkRoomCabinet = new CheckRoomCabinet();
            checkRoomCabinet.setPlanId(cabinetSubBean.getPlanId());
            checkRoomCabinet.setCabinetId(cabinetUUID);
            checkRoomCabinet.setRoomId(planContrastEquCabinet.getMacId());
            checkRoomCabinet.setStatus("1");
            checkRoomCabinetMapper.insert(checkRoomCabinet);

        }
        //判断根据计划Id查询该计划下所有机柜核验结束则更改计划机房表内核验状态
        Integer count = checkRoomCabinetMapper.selectCount(new QueryWrapper<CheckRoomCabinet>().lambda().eq(CheckRoomCabinet::getPlanId, cabinetSubBean.getPlanId()).eq(CheckRoomCabinet::getRoomId, planContrastEquCabinet.getMacId()).eq(CheckRoomCabinet::getStatus, "0"));
        if (count == 0) {
            CheckPlanRoom checkPlanRoom = new CheckPlanRoom();
            checkPlanRoom.setPlanId(cabinetSubBean.getPlanId());
            checkPlanRoom.setRoomId(planContrastEquCabinet.getMacId());
            checkPlanRoom.setStatus("1");
            checkPlanRoomMapper.update(checkPlanRoom, new QueryWrapper<CheckPlanRoom>().lambda().eq
                    (CheckPlanRoom::getPlanId, cabinetSubBean.getPlanId()).eq(CheckPlanRoom::getRoomId, planContrastEquCabinet
                    .getMacId()));

            //根据计划Id查询计划下所有机房核验情况若机房全部核验则更改计划表内计划核验状态
            Integer count1 = checkPlanRoomMapper.selectCount(new QueryWrapper<CheckPlanRoom>().lambda().eq(CheckPlanRoom::getPlanId, cabinetSubBean.getPlanId()).eq(CheckPlanRoom::getStatus, "0"));
            if (count1 == 0) {
                CheckPlan checkPlan = new CheckPlan();
                checkPlan.setUuid(cabinetSubBean.getPlanId());
                checkPlan.setStatus("1");
                checkPlanRoomMapper.update(checkPlanRoom, new QueryWrapper<CheckPlanRoom>().lambda().eq
                        (CheckPlanRoom::getPlanId, cabinetSubBean.getPlanId()));
            }
        }
    }

    @Override
    public List<EquipmentType> getAllType() {
        List<EquipmentType> equipmentTypes = equipmentTypeMapper.selectList(new QueryWrapper<EquipmentType>());
        return equipmentTypes;
    }

    /**
     * cad导入机柜
     *
     * @param list cad机柜数据
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean cadAdd(List<GeoCabinet> list) {
        //批量插入机柜表
        List<EquCabinet> cabinets = new ArrayList<>();
        List<MacShow> pointsList = new ArrayList<>();

        List<GeoCabinet> offsetList = offset(list);

        for (GeoCabinet geoCabinet : offsetList) {
            EquCabinet info = getOne(Wrappers.<EquCabinet>lambdaQuery().like(EquCabinet::getCabinetName, geoCabinet.getName()).last("and rownum = 1"));
            EquCabinet cabinet = new EquCabinet();
            if (info == null) {
                cabinet.setUuid(IdUtil.simpleUUID());
                cabinet.setCabinetName(geoCabinet.getName());
                cabinet.setMacId(geoCabinet.getMacId());
                cabinet.setCreateBy(SecurityUtils.getUsername());
                cabinet.setCreateTime(DateUtil.now());
                cabinets.add(cabinet);
            }

            MacShow macShow = new MacShow();
            macShow.setMacId(geoCabinet.getMacId());
            macShow.setCabinetId(info == null ? cabinet.getUuid() : info.getUuid());
            macShow.setCreateTime(DateUtil.now());

            Node node = resizeNode(geoCabinet);
            node.setX(geoCabinet.getCenter().x);
            node.setY(geoCabinet.getCenter().y);
            node.setLabel(geoCabinet.getName());
            double radian = geoCabinet.getRadian() * 0.01745329251994329555555555555556;
            node.setRadian(radian + "");
            macShow.setNode(JSONUtil.toJsonStr(node));

            pointsList.add(macShow);
        }


        saveBatch(cabinets);
        //清空展示表
        pointsService.remove(Wrappers.<MacShow>lambdaQuery().eq(MacShow::getMacId, offsetList.get(0).getMacId()));
        //插入展示表
        pointsService.saveBatch(pointsList);
        return true;
    }

    @Override
    public void editNoOrder(CabinetSubBean cabinetSubBean) {
        String cabinetId = cabinetSubBean.getCabinet().getUuid();
        if (cabinetId.contains("local-")) {
            List<EquipmentDetailDto> equipment = cabinetSubBean.getEquipment();
            ContrastEquCabinet cabinet = cabinetSubBean.getCabinet();
            EquCabinet equCabinet = new EquCabinet();
            BeanUtils.copyProperties(cabinet, equCabinet);
            equCabinetMapper.insert(equCabinet);
            for (EquipmentDetailDto equipmentDetailDto : equipment) {
                Equipment equipment1 = new Equipment();
                BeanUtils.copyProperties(equipmentDetailDto, equipment1);
                equipment1.setUuid(UUID.randomUUID().toString());
                equipment1.setCabinetId(cabinetId);
                equipmentMapper.insert(equipment1);
                EquipmentDetail contrastEquipmentDetail = new EquipmentDetail();
                copyEquipmentDetail(equipmentDetailDto, contrastEquipmentDetail);
                contrastEquipmentDetail.setUuid(equipment1.getUuid());
                contrastEquipmentDetail.setCabinetId(cabinetId);
                contrastEquipmentDetail.setEquId(equipment1.getUuid());
                contrastEquipmentDetail.setCabinetName(cabinetSubBean.getCabinet().getCabinetName());
                //核验设备数据并插入
                equipmentDetailMapper.insert(contrastEquipmentDetail);
            }
        } else {
            List<EquipmentDetailDto> equipment = cabinetSubBean.getEquipment();
            ContrastEquCabinet cabinet = cabinetSubBean.getCabinet();
            EquCabinet equCabinet = new EquCabinet();
            BeanUtils.copyProperties(cabinet, equCabinet);
            equCabinetMapper.updateById(equCabinet);
            for (EquipmentDetailDto equipmentDetailDto : equipment) {
                Equipment equipment1 = new Equipment();
                BeanUtils.copyProperties(equipmentDetailDto, equipment1);
                if (StringUtils.isBlank(equipment1.getUuid())) {
                    equipment1.setUuid(UUID.randomUUID().toString());
                    equipment1.setCabinetId(cabinetId);
                    equipmentMapper.insert(equipment1);
                    EquipmentDetail contrastEquipmentDetail = new EquipmentDetail();
                    copyEquipmentDetail(equipmentDetailDto, contrastEquipmentDetail);
                    contrastEquipmentDetail.setUuid(equipment1.getUuid());
                    contrastEquipmentDetail.setCabinetId(cabinetId);
                    contrastEquipmentDetail.setEquId(equipment1.getUuid());
                    contrastEquipmentDetail.setCabinetName(cabinetSubBean.getCabinet().getCabinetName());
                    //核验设备数据并插入
                    equipmentDetailMapper.insert(contrastEquipmentDetail);
                } else {
                    equipmentMapper.updateById(equipment1);
                    EquipmentDetail contrastEquipmentDetail = equipmentDetailMapper.selectByEquId(equipment1.getUuid());
                    boolean isAdd = false;
                    if (contrastEquipmentDetail == null) {
                        contrastEquipmentDetail=new EquipmentDetail();
                        contrastEquipmentDetail.setUuid(UUID.randomUUID().toString());
                        isAdd = true;
                    }
                    copyEquipmentDetail(equipmentDetailDto, contrastEquipmentDetail);
                    contrastEquipmentDetail.setCabinetId(cabinetId);
                    contrastEquipmentDetail.setEquId(equipment1.getUuid());
                    contrastEquipmentDetail.setCabinetName(cabinetSubBean.getCabinet().getCabinetName());
                    //核验设备数据并插入
                    if (isAdd) {
                        equipmentDetailMapper.insert(contrastEquipmentDetail);
                    } else {
                        equipmentDetailMapper.updateById(contrastEquipmentDetail);
                    }

                }

            }
        }

    }

    @Override
    public Map<String, Object> getDetialById(EquCabinet equCabinet, String planId) {
        QueryWrapper<ContrastEquCabinet> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(ContrastEquCabinet::getCabinetId,equCabinet.getUuid());
        queryWrapper.lambda().eq(ContrastEquCabinet::getPlanId,planId);
        List<ContrastEquCabinet> contrastEquCabinets = contrastEquCabinetMapper.selectList(queryWrapper);
        Map<String, Object> result = new HashMap<>();
        if(contrastEquCabinets.isEmpty()){
            equCabinet = equCabinetMapper.selectById(equCabinet.getUuid());
            if (equCabinet != null) {
                List<Energy> energyList = energyMapper.selectList(new QueryWrapper<Energy>().lambda().eq(Energy::getCabinetId, equCabinet.getUuid()));
                if (!energyList.isEmpty()) {
                    result.put("energy", energyList.get(0));
                }
                List<EquipmentDetailDto> equipment = equipmentDetailMapper.selectByCabinetId(equCabinet.getUuid());
                result.put("equipment", equipment);
            }
            result.put("cabinet", equCabinet);

        }else{
            ContrastEquCabinet contrastEquCabinet = contrastEquCabinets.get(0);
            contrastEquCabinet.setUuid(contrastEquCabinet.getCabinetId());
            result.put("cabinet", contrastEquCabinet);
            QueryWrapper<ContrastEquipmentDetail> contrastEquipmentDetailQueryWrapper=new QueryWrapper<>();
            contrastEquipmentDetailQueryWrapper.lambda().eq(ContrastEquipmentDetail::getCabinetId,equCabinet.getUuid()).eq(ContrastEquipmentDetail::getPlanId,planId);
            List<ContrastEquipmentDetail> contrastEquipmentDetails = contrastEquipmentDetailMapper.selectList(contrastEquipmentDetailQueryWrapper);
            result.put("equipment", contrastEquipmentDetails);
        }
        return result;
    }

    private void copyEquipmentDetail(EquipmentDetailDto equipmentDetailDto, EquipmentDetail contrastEquipmentDetail) {
        contrastEquipmentDetail.setAssetsBigType(equipmentDetailDto.getAssetsBigType());
        contrastEquipmentDetail.setAssetsSmallType(equipmentDetailDto.getAssetsSmallType());
        contrastEquipmentDetail.setAssetsNumber(equipmentDetailDto.getAssetsNumber());
        contrastEquipmentDetail.setOwnerSystem(equipmentDetailDto.getOwnerSystem());
        contrastEquipmentDetail.setModelType(equipmentDetailDto.getModelType());
        contrastEquipmentDetail.setAssetsState(equipmentDetailDto.getAssetsState());
        contrastEquipmentDetail.setPrincipal(equipmentDetailDto.getPrincipal());
        contrastEquipmentDetail.setDepartment(equipmentDetailDto.getDepartment());
        contrastEquipmentDetail.setOffice(equipmentDetailDto.getOffice());
        contrastEquipmentDetail.setMacRoomName(equipmentDetailDto.getMacRoomName());
        contrastEquipmentDetail.setCabinetId(equipmentDetailDto.getCabinetId());
        contrastEquipmentDetail.setOpenDate(equipmentDetailDto.getOpenDate());
        contrastEquipmentDetail.setUfloorNum(equipmentDetailDto.getUfloorNum());
        contrastEquipmentDetail.setCabinetName(equipmentDetailDto.getCabinetName());
        contrastEquipmentDetail.setEquId(equipmentDetailDto.getEquId());
        contrastEquipmentDetail.setEquName(equipmentDetailDto.getEquName());
        contrastEquipmentDetail.setEquHeight(equipmentDetailDto.getEquHeight());
        contrastEquipmentDetail.setUpdateTime(DateUtil.now());
        contrastEquipmentDetail.setCreateTime(DateUtil.now());
    }

    /**
     * 偏移坐标
     */
    private List<GeoCabinet> offset(List<GeoCabinet> list) {
        List<GeoCabinet> geoCabinetList = new ArrayList<>();
        //获得最小x，y
        double minX = 0;
        double minY = 0;
        for (GeoCabinet geoCabinet : list) {
            if (geoCabinet != null && geoCabinet.getCenter() != null) {
                if (minX == 0) {
                    minX = geoCabinet.getCenter().x;
                } else {
                    if (geoCabinet.getCenter().x < minX) {
                        minX = geoCabinet.getCenter().x;
                    }
                }
                if (minY == 0) {
                    minY = geoCabinet.getCenter().y;
                } else {
                    if (geoCabinet.getCenter().y < minY) {
                        minY = geoCabinet.getCenter().y;
                    }
                }
            }
        }

        //偏移坐标
        for (GeoCabinet geoCabinet : list) {
            if (geoCabinet != null && geoCabinet.getCenter() != null) {
                double x = geoCabinet.getCenter().x - minX;
                //高度等比缩小20倍
                double y = (minY - geoCabinet.getCenter().y) / 20;
                //初始坐标从0.0 +100
                geoCabinet.getCenter().x = (x > 800 ? (x / 10) : x) + 100;
                geoCabinet.getCenter().y = (y > 800 ? (y / 10) : y) + 100;
                geoCabinetList.add(geoCabinet);
            }
        }

        return geoCabinetList;
    }

    /**
     * 根据坐标计算块的宽高比，返回等比例node大小
     */
    private Node resizeNode(GeoCabinet geoCabinet) {
        List<Point2D.Double> points = geoCabinet.getPoints();
        Point2D.Double p1 = points.get(0);
        Point2D.Double p2 = points.get(1);
        Point2D.Double p3 = points.get(2);
        Point2D.Double p4 = points.get(3);
        double width = Math.abs(p1.x - p2.x);
        double height = Math.abs(p3.y - p4.y);

        Node node = new Node();
        node.setWidth((int) (width / 20));
        node.setHeight((int) (height / 20));
        node.setSize(new double[]{node.getWidth(), node.getHeight()});
        //默认的机柜矩形
        if ((width + "").equals(height + "")) {
            node.setType("rectangle");
        } else {
            //正方形
            node.setType("square");
        }
        return node;
    }

    @Override
    public List<EquCabinetNode> findEquCabinetNodeByMacId(String macId) {
        return equCabinetMapper.findEquCabinetNodeByMacId(macId);
    }

////    @Override
//    public void appEdit(CabinetSubBean cabinetSubBean) {
//        ContrastEquCabinet equCabinet = cabinetSubBean.getCabinet();
//        equCabinet.setEnergyName(cabinetSubBean.getEnergy().getEnergyName());
//        String cabinetUUID = UUID.randomUUID().toString().trim().replaceAll("-", "");
//        if (StringUtils.isNotBlank(equCabinet.getUuid())) {
//            equCabinet.setUpdateTime(DateUtil.now());
//            cabinetUUID = equCabinet.getUuid();
//            CheckRoomCabinet checkRoomCabinet = new CheckRoomCabinet();
//            checkRoomCabinet.setPlanId(cabinetSubBean.getPlanId());
//            checkRoomCabinet.setCabinetId(cabinetUUID);
//            checkRoomCabinet.setRoomId(equCabinet.getMacId());
//            checkRoomCabinet.setStatus("1");
//            checkRoomCabinetMapper.update(checkRoomCabinet, new QueryWrapper<CheckRoomCabinet>().lambda().eq(CheckRoomCabinet::getPlanId, cabinetSubBean.getPlanId()).eq(CheckRoomCabinet::getCabinetId, equCabinet.getUuid()));
//        } else {
//            CheckRoomCabinet checkRoomCabinet = new CheckRoomCabinet();
//            checkRoomCabinet.setPlanId(cabinetSubBean.getPlanId());
//            checkRoomCabinet.setCabinetId(cabinetUUID);
//            checkRoomCabinet.setRoomId(equCabinet.getMacId());
//            checkRoomCabinet.setStatus("1");
//            checkRoomCabinetMapper.insert(checkRoomCabinet);
//
//        }
//        Integer count = checkRoomCabinetMapper.selectCount(new QueryWrapper<CheckRoomCabinet>().lambda().eq(CheckRoomCabinet::getPlanId, cabinetSubBean.getPlanId()).eq(CheckRoomCabinet::getRoomId, equCabinet.getMacId()).eq(CheckRoomCabinet::getStatus, "0"));
//        if (count == 0) {
//            CheckPlanRoom checkPlanRoom = new CheckPlanRoom();
//            checkPlanRoom.setPlanId(cabinetSubBean.getPlanId());
//            checkPlanRoom.setRoomId(equCabinet.getMacId());
//            checkPlanRoom.setStatus("1");
//            checkPlanRoomMapper.update(checkPlanRoom, new QueryWrapper<CheckPlanRoom>().lambda().eq(CheckPlanRoom::getPlanId, cabinetSubBean.getPlanId()).eq(CheckPlanRoom::getRoomId, equCabinet.getMacId()));
//
//
//            Integer count1 = checkPlanRoomMapper.selectCount(new QueryWrapper<CheckPlanRoom>().lambda().eq(CheckPlanRoom::getPlanId, cabinetSubBean.getPlanId()).eq(CheckPlanRoom::getStatus, "0"));
//            if (count1 == 0) {
//                CheckPlan checkPlan = new CheckPlan();
//                checkPlan.setUuid(cabinetSubBean.getPlanId());
//                checkPlan.setStatus("1");
//                checkPlanRoomMapper.update(checkPlanRoom, new QueryWrapper<CheckPlanRoom>().lambda().eq(CheckPlanRoom::getPlanId, cabinetSubBean.getPlanId()));
//            }
//        }
//    }
}
